home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / NumericTest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.4 KB  |  43 lines

  1. //: C21:NumericTest.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include "PrintSequence.h"
  7. #include <numeric>
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <iterator>
  11. #include <functional>
  12. using namespace std;
  13.  
  14. int main() {
  15.   int a[] = { 1, 1, 2, 2, 3, 5, 7, 9, 11, 13 };
  16.   const int asz = sizeof a / sizeof a[0];
  17.   print(a, a + asz, "a", " ");
  18.   int r = accumulate(a, a + asz, 0);
  19.   cout << "accumulate 1: " << r << endl;
  20.   // Should produce the same result:
  21.   r = accumulate(a, a + asz, 0, plus<int>());
  22.   cout << "accumulate 2: " << r << endl;
  23.   int b[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2 };
  24.   print(b, b + sizeof b / sizeof b[0], "b", " ");
  25.   r = inner_product(a, a + asz, b, 0);
  26.   cout << "inner_product 1: " << r << endl;
  27.   // Should produce the same result:
  28.   r = inner_product(a, a + asz, b, 0, 
  29.     plus<int>(), multiplies<int>());
  30.   cout << "inner_product 2: " << r << endl;
  31.   int* it = partial_sum(a, a + asz, b);
  32.   print(b, it, "partial_sum 1", " ");
  33.   // Should produce the same result:
  34.   it = partial_sum(a, a + asz, b, plus<int>());
  35.   print(b, it, "partial_sum 2", " ");
  36.   it = adjacent_difference(a, a + asz, b);
  37.   print(b, it, "adjacent_difference 1"," ");
  38.   // Should produce the same result:
  39.   it = adjacent_difference(a, a + asz, b, 
  40.     minus<int>());
  41.   print(b, it, "adjacent_difference 2"," ");
  42. } ///:~
  43.